'use strict'

class manage {
    constructor () {

    }
    async list(c) { //  活动列表展示路由
        let sqltext = `SELECT * FROM activity`
        try {
            let r = await c.service.psql.query(sqltext,[]);
            c.send(JSON.stringify(r.rows))
            
        } catch (err) {
            c.status(500).send("Database's operations failed!")
        }
    }
    async get(c) { //单个活动展示路由
        let sqltext = `SELECT * FROM activity WHERE "activityId"=$1`
        try {
            let r = await c.service.psql.query(sqltext,[c.param.id]);
            if (r.rowCount != 0) { 
                c.send(JSON.stringify(r.rows))
            } else {
                c.status(500).send('Show activity failed!')
            }
        } catch (err) {
            c.status(500).send("Database's operations failed!")
        }
    }
    async post(c) { //活动更新/添加路由
        console.log('执行了post manage');
        // 此处应写一个判断，即如果有图片则需要去保存图片且保存图片路径
        let isUpdate = c.headers.update;
        console.log("isUpdate",isUpdate);
        if (isUpdate) { //更新活动
            
            let f = c.getFile('image');
            console.log('f：',f)
            console.log('!f：',!f)
            if (!f) { //未获取到图片
                
                // 保存文字
                try {
                    let sqltext = `UPDATE activity SET "shortTitle"=$1,"activityTime"=$2,"activitySpot"=$3,`
                                    +`"activityContent"=$4,"fullTitle"=$5 WHERE "activityId"=$6`;
                    // console.log(c)
                    console.log('----------------------------------------------------------------')
                    console.log(c.body)
                    console.log('----------------------------------------------------------------')
                    console.log('c.body.toString()：',c.body.toString())
                    let data = JSON.parse(c.body.toString());
                    console.log(typeof data)
                    console.log('标志1')
                    console.log(typeof data)
                    console.log(data);
                    let r = await c.service.psql.query(sqltext,[data.shortTitle,data.activityTime,data.activitySpot,data.activityContent,data.fullTitle,data.activityId]);
                    console.log('标志2')
                    if (r.rowCount != 0) { 
                        c.send('Add activity successfully!')
                    } else {
                        c.status(500).send('Add activity failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
            }
            else{ //获取到图片
                // console.log(c)
                console.log('----------------------------------------------------------------')
                console.log('c.body：',c.body)
                console.log('----------------------------------------------------------------')
                console.log('c.body.toString()：',c.body.toString())
                // 保存图片文件
                let fname = c.helper.makeName(f.filename);
                let filePath = `${c.service.imagePath}/${fname}`;
                await c.moveFile(f,filePath);
                // 保存文字和图片路径
                try {
                    
                    let data = JSON.parse(c.body.toString());
                    console.log(data);
                    let sqltext = `UPDATE activity SET "shortTitle"=$1,"activityTime"=$2,`
                                +`"activitySpot"=$3,"activityContent"=$4,"activityImg"=$5 WHERE "activityId"=$6`;
                    let r = await c.service.psql.query(sqltext,[data.shortTitle,data.activityTime,data.activitySpot,data.activityContent,filePath,data.activityId]);
                    if (r.rowCount != 0) { 
                        c.send('Add activity successfully!')
                    } else {
                        c.status(500).send('Add activity failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
            }
        }
        else{ // 添加活动
            let f = c.getFile('image');
            console.log('f：',f)
            console.log('!f：',!f)
            if (!f) { //未获取到图片
                let sqltext = `INSERT INTO activity("shortTitle","activityTime","activitySpot","activityContent")`  
                            +`VALUES($1,$2,$3,$4)`;
                // 保存文字
                try {
                    // console.log(c)
                    console.log('----------------------------------------------------------------')
                    console.log(c.body)
                    console.log('----------------------------------------------------------------')
                    console.log('c.body.toString()：',c.body.toString())
                    let data = JSON.parse(c.body.toString());
                    console.log('标志1')
                    console.log(data);
                    let r = await c.service.psql.query(sqltext,[data.shortTitle,data.activityTime,data.activitySpot,data.activityContent]);
                    console.log('标志2')
                    if (r.rowCount != 0) { 
                        c.send('Add activity successfully!')
                    } else {
                        c.status(500).send('Add activity failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
            }
            else{ //获取到图片
                // console.log(c)
                console.log('----------------------------------------------------------------')
                console.log('c.body：',c.body)
                console.log('----------------------------------------------------------------')
                console.log('c.body.toString()：',c.body.toString())
                // 保存图片文件
                let fname = c.helper.makeName(f.filename);
                let filePath = `${c.service.imagePath}/${fname}`;
                await c.moveFile(f,filePath);
                // 保存文字和图片路径
                try {
                    
                    let data = JSON.parse(c.body.toString());
                    console.log(data);
                    let sqltext = `INSERT INTO activity("shortTitle","activityImg","activityTime","activitySpot","activityContent")`  // 此处应写一个判断，即如果有图片则需要去保存图片且保存图片路径
                                +`VALUES($1,$2,$3,$4,$5)`;
                    let r = await c.service.psql.query(sqltext,[data.shortTitle,filePath,data.activityTime,data.activitySpot,data.activityContent]);
                    if (r.rowCount != 0) { 
                        c.send('Add activity successfully!')
                    } else {
                        c.status(500).send('Add activity failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
            }
        
        }

        
    }
    async delete(c) { //活动删除路由
        let sqltext = `DELETE FROM activity WHERE "activityId"=$1`
        try {
            let r = await c.service.psql.query(sqltext,[c.param.id]);
            if (r.rowCount != 0) { 
                c.send(JSON.stringify('Delete activity successfully!'))
            } else {
                c.status(500).send('Delete activity failed!')
            }
        } catch (err) {
            c.status(500).send("Database's operations failed!")
        }
    }

}
module.exports = manage;